package com.wn.common.utils;

import com.wn.common.exception.Assert;
import com.wn.common.model.vo.user.TokenVo;
import com.wn.common.result.enums.impl.ResultCode;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yellow Docter
 * @date 2023 -03-01
 * @desc
 */
public class TokenUtils {

    /**
     * 通过token 获取用户的 信息
     * @return
     */
    public static TokenVo getTokenVo(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest req = servletRequestAttributes.getRequest();
        String userId = req.getHeader("userId");
        String phone = req.getHeader("phone");

        //token 中没有内容 抛出异常
        Assert.error(userId, ResultCode.PERMISSION_NO_ACCESS);
        return TokenVo.builder().userId(userId).phone(phone).build();
    }

}
